using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using System.Collections;
using System.Windows.Controls;
using _3DHouse.CustomControl;

namespace _3DHouse.Lib
{
   public  class DrawFloor
    {
       /// <summary>
       /// 横线
       /// </summary>
       /// <param name="point1">左边点</param>
       /// <param name="point2">右边点</param>
       /// <param name="color">线的颜色</param>
       /// <returns></returns>
       public static GeometryModel3D TransverseLine(Point3D point1, Point3D point2, Color color)
        {
            Point3D p1 = new Point3D(point1.X,point1.Y,point1.Z);
            Point3D p2 = new Point3D(point1.X , point1.Y , point1.Z + 0.01);
            Point3D p3 = new Point3D(point2.X, point2.Y, point2.Z );
            Point3D p4 = new Point3D(point2.X, point2.Y, point2.Z + 0.01);
            Point3DCollection positions = new Point3DCollection(4);
            positions.Add(p1);
            positions.Add(p2);
            positions.Add(p3);
            positions.Add(p4);
            Int32Collection indices = new Int32Collection(6);
            indices.Add(0);
            indices.Add(1);
            indices.Add(3);
            indices.Add(3);
            indices.Add(2);
            indices.Add(0);
            MeshGeometry3D mesh = new MeshGeometry3D();
            mesh.Positions = positions;
            mesh.TriangleIndices = indices;
              GeometryModel3D _model=new GeometryModel3D();
            
             _model = new GeometryModel3D();
             _model.Geometry = mesh;
           DiffuseMaterial diff = new DiffuseMaterial();
           diff.Brush = new SolidColorBrush(color);
              _model.Material = diff;
             return _model;
        }
       /// <summary>
       /// 竖线
       /// </summary>
       /// <param name="point1">后边点</param>
       /// <param name="point2">前边点</param>
       /// <param name="color">线的颜色</param>
       /// <returns></returns>
       public static GeometryModel3D VerticalLine(Point3D point1, Point3D point2, Color color)
       {
           Point3D p1 = new Point3D(point1.X , point1.Y, point1.Z);
           Point3D p2 = new Point3D(point1.X+0.01, point1.Y, point1.Z );
           Point3D p3 = new Point3D(point2.X , point2.Y, point2.Z );
           Point3D p4 = new Point3D(point2.X+0.01, point2.Y, point2.Z );
           Point3DCollection positions = new Point3DCollection(4);
           positions.Add(p1);
           positions.Add(p2);
           positions.Add(p3);
           positions.Add(p4);
           Int32Collection indices = new Int32Collection(6);
           indices.Add(0);
           indices.Add(2);
           indices.Add(3);
           indices.Add(3);
           indices.Add(1);
           indices.Add(0);
           MeshGeometry3D mesh = new MeshGeometry3D();
           mesh.Positions = positions;
           mesh.TriangleIndices = indices;
           GeometryModel3D _model = new GeometryModel3D();

           _model = new GeometryModel3D();
           _model.Geometry = mesh;
           DiffuseMaterial diff = new DiffuseMaterial();

           diff.Brush = new SolidColorBrush(color);
           _model.Material = diff;
           return _model;
       }
       /// <summary>
       /// 画地板线
       /// </summary>
       /// <param name="num">地板宽度</param>
       /// <returns></returns>
       public static ModelVisual3D WriteLine(int num)
       {
           Color color = Colors.Black;//Color color = (Color)ColorConverter.ConvertFromString("");
           ModelVisual3D model = new ModelVisual3D();
           Model3DGroup group=new Model3DGroup();
           model.Content=group;
           int startIndex=-num/2;
           int endIndex=num/2;
           for (int n=startIndex; n < endIndex; n++)
           {
               if (n == 0)
               {
                   color = Colors.Red;
               }
               else
               {
                   color = Colors.Black;
               }
               group.Children.Add(TransverseLine(new Point3D(startIndex, 0, n), new Point3D(endIndex, 0, n),color));
           }
           for (int n = startIndex; n < endIndex; n++)
           {
               if (n == 0)
               {
                   color = Colors.Blue;
               }
               else
               {
                   color = Colors.Black;
               }
               group.Children.Add(VerticalLine(new Point3D(n, 0, startIndex), new Point3D(n, 0, endIndex), color));
           }
           return model;
       }
       public static ModelVisual3D WriteFloor(int witdh)
       {
           Color color = Colors.Green;
           ModelVisual3D model = new ModelVisual3D();
           int startIndex = -witdh / 2;
           int endIndex = witdh / 2;
          // Point3D point1=new Point3D(startIndex,-0.01,startIndex);
          // Point3D point2=new Point3D(startIndex,-0.01,endIndex);
           Point3D p1 = new Point3D(startIndex, -0.01, startIndex);
           Point3D p2 = new Point3D(startIndex + witdh, -0.01, startIndex);
           Point3D p3 = new Point3D(startIndex, -0.01, endIndex);
           Point3D p4 = new Point3D(startIndex + witdh, -0.01, endIndex);
           Point3DCollection positions = new Point3DCollection(4);
           positions.Add(p1);
           positions.Add(p2);
           positions.Add(p3);
           positions.Add(p4);
           Int32Collection indices = new Int32Collection(6);
           indices.Add(0);
           indices.Add(2);
           indices.Add(3);
           indices.Add(3);
           indices.Add(1);
           indices.Add(0);
           MeshGeometry3D mesh = new MeshGeometry3D();
           mesh.Positions = positions;
           mesh.TriangleIndices = indices;
           GeometryModel3D geometry = new GeometryModel3D();

           geometry = new GeometryModel3D();
           geometry.Geometry = mesh;
           DiffuseMaterial diff = new DiffuseMaterial(new SolidColorBrush(Colors.LightSteelBlue));
           SpecularMaterial spec = new SpecularMaterial(new SolidColorBrush(color),2);
           MaterialGroup materialGroup = new MaterialGroup();
           materialGroup.Children.Add(diff);
           materialGroup.Children.Add(spec);
           geometry.Material = materialGroup;
           model.Content = geometry;
           return model;
       }
       public static ModelVisual3D WriteFloor(double witdh, double length)
       {
           Color color = Colors.Green;
           ModelVisual3D model = new ModelVisual3D();
           double x = witdh / 2;
           double z = length / 2;
          // Point3D point1 = new Point3D(-x, -0.01, -z);
          // Point3D point2 = new Point3D(-x, -0.01, z);
           Point3D p1 = new Point3D(-x, -0.01, -z);
           Point3D p2 = new Point3D(-x + witdh, -0.01, -z);
           Point3D p3 = new Point3D(-x, -0.01, z);
           Point3D p4 = new Point3D(--x + witdh, -0.01, z);
           Point3DCollection positions = new Point3DCollection(4);
           positions.Add(p1);
           positions.Add(p2);
           positions.Add(p3);
           positions.Add(p4);
           Int32Collection indices = new Int32Collection(6);
           indices.Add(0);
           indices.Add(2);
           indices.Add(3);
           indices.Add(3);
           indices.Add(1);
           indices.Add(0);
           MeshGeometry3D mesh = new MeshGeometry3D();
           mesh.Positions = positions;
           mesh.TriangleIndices = indices;
           GeometryModel3D geometry = new GeometryModel3D();

           geometry = new GeometryModel3D();
           geometry.Geometry = mesh;
           DiffuseMaterial diff = new DiffuseMaterial(new SolidColorBrush(Colors.LightSteelBlue));
           SpecularMaterial spec = new SpecularMaterial(new SolidColorBrush(color), 2);
           MaterialGroup materialGroup = new MaterialGroup();
           materialGroup.Children.Add(diff);
           materialGroup.Children.Add(spec);
           geometry.Material = materialGroup;
           model.Content = geometry;
           return model;
       }
     
       public static ModelVisual3D WriteFloor(double left, double right ,double front ,double back)
       {
           Color color = Colors.Green;
           ModelVisual3D model = new ModelVisual3D();        
           //左后
           Point3D p1 = new Point3D(left, -0.01, back);
           //右后
           Point3D p2 = new Point3D(right, -0.01, back);
           //左前
           Point3D p3 = new Point3D(left, -0.01, front);
           //右前
           Point3D p4 = new Point3D(right, -0.01, front);
           Point3DCollection positions = new Point3DCollection(4);
           positions.Add(p1);
           positions.Add(p2);
           positions.Add(p3);
           positions.Add(p4);
           Int32Collection indices = new Int32Collection(6);
           indices.Add(0);
           indices.Add(2);
           indices.Add(3);
           indices.Add(3);
           indices.Add(1);
           indices.Add(0);
           MeshGeometry3D mesh = new MeshGeometry3D();
           mesh.Positions = positions;
           mesh.TriangleIndices = indices;
           GeometryModel3D geometry = new GeometryModel3D();

           geometry = new GeometryModel3D();
           geometry.Geometry = mesh;
           DiffuseMaterial diff = new DiffuseMaterial(new SolidColorBrush(Colors.LightSteelBlue));
           SpecularMaterial spec = new SpecularMaterial(new SolidColorBrush(color), 2);
           MaterialGroup materialGroup = new MaterialGroup();
           materialGroup.Children.Add(diff);
           materialGroup.Children.Add(spec);
           geometry.Material = materialGroup;
           model.Content = geometry;
           return model;
       }
       public static ModelVisual3D WriteFloor(Viewport3D world)
       {
           ArrayList arrX = new ArrayList();  //x坐标集合                
           ArrayList arrZ = new ArrayList();//z坐标集合
           //截取地板
           foreach (Visual3D visual in world.Children)
           {
               if (visual is Cube)
               {
                   Cube cube = visual as Cube;
                   arrX.Add(cube.Content.Bounds.X);
                   arrX.Add(cube.Content.Bounds.X + cube.Content.Bounds.SizeX);
                   arrZ.Add(cube.Content.Bounds.Z);
                   arrZ.Add(cube.Content.Bounds.Z + cube.Content.Bounds.SizeZ);
               }
           }
           if (arrX.Count == 0 || arrZ.Count==0)
           {
               return null;
           }
           arrX.Sort();//排序
           arrZ.Sort();
           //获得所有模型的总宽度
           double left = (double)arrX[0];
           double right = (double)arrX[arrX.Count - 1];
           double front = (double)arrZ[arrZ.Count - 1];
           double back = (double)arrZ[0];
           ModelVisual3D floor = DrawFloor.WriteFloor(left, right, front, back);
           return floor;
       }
    }
}
